package com.sniper.web.freemarker;

import com.sniper.model.Files;
import com.sniper.utils.FfmpegUtils;
import com.sniper.utils.FileIconUtil;
import com.sniper.utils.HtmlUtil;
import com.sniper.utils.PostUtil;
import com.sniper.web.utils.FilesUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.oro.text.regex.MalformedPatternException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * freemarker一些集成的一些方法
 *
 * @author suzhen
 */
public class FreeMarkerViewUtil {

    private final static Logger LOGGER = LoggerFactory.getLogger(FreeMarkerUtil.class);

    /**
     * 截取字符串
     *
     * @param str
     * @param limit
     * @return
     */
    public static String substring(String str, int limit) {
        str = delHTMLTag(str);
        if (str.length() > limit) {
            return StringUtils.substring(str, 0, limit);
        }
        return str;
    }

    /**
     * 速度图片
     *
     * @param files
     * @return
     */
    public static PostUtil.BackImg getImg(String[] files) {
        return PostUtil.getImg(files);
    }


    /**
     * 清除html
     *
     * @param html
     * @return
     */
    public static String delHTMLTag(String html) {
        return HtmlUtil.removeHTML(html);
    }

    /**
     * 删除所有非字母数字字符
     *
     * @param html
     * @return
     */
    public static String delNotText(String html) {
        return HtmlUtil.removeNotText(html);
    }

    /**
     * 自定义的截取字符串
     *
     * @param str
     * @param limit
     * @param suffix
     * @return
     */
    public static String substring(String str, int limit, String suffix) {

        str = delHTMLTag(str);
        int length = str.length();
        int cutPos;
        if (length >= (limit + suffix.length())) {
            cutPos = limit;
            return StringUtils.substring(str, 0, cutPos) + suffix;
        }
        return str;
    }

    public static String subTextString(String str, int len) {
        if (str.length() < len / 2) return str;
        int count = 0;
        len *= 2;
        StringBuilder sb = new StringBuilder();
        String[] ss = str.split("");
        for (int i = 1; i < ss.length; i++) {
            count += ss[i].getBytes().length > 1 ? 2 : 1;
            sb.append(ss[i]);
            if (count >= len) break;
        }
        //不需要显示...的可以直接return sb.toString();
        return (sb.toString().length() < str.length()) ? sb.append("...").toString() : str;
    }

    /**
     * 截取视频的截图
     *
     * @param videoPath
     * @param widthAndHeight
     * @return
     */
    public static String getFace(String videoPath, String widthAndHeight) {

        String defaultPath = "/myfiles/images/default/company-default.png";

        // 获取附件根目录地址
        String rootDir = "";
        if (!videoPath.startsWith("http")) {
            rootDir = FilesUtil.getRootDir();
            // 去掉最后的斜杠
            if (rootDir.endsWith("/")) {
                rootDir = rootDir.substring(0, rootDir.length() - 1);
            }
            // 查看文件是否存在
            String filePath = rootDir + videoPath;
            File fileTemp = new File(filePath);
            if (!fileTemp.isFile()) {
                return defaultPath;
            }
        }


        // 开始截图
        FfmpegUtils ffmpegUtils = new FfmpegUtils(rootDir + videoPath);
        if (!widthAndHeight.equals("")) {
            ffmpegUtils.setCutWidthAndHeight(widthAndHeight);
        }
        // 查看截图是否已经存在
        String savePath = ffmpegUtils.getSavePath(".png");
        File file = new File(rootDir + savePath);
        if (file.isFile()) {
            return savePath.replaceFirst(rootDir, "");
        }
        try {
            ffmpegUtils.setOutPath(savePath).videoScreenshot();
        } catch (MalformedPatternException e) {
            e.printStackTrace();
            LOGGER.error(e.getMessage(), e);
        }
        return savePath.replace(rootDir, "");
    }

    /**
     * 截取图片
     *
     * @param path
     * @param width
     * @param height
     * @return
     */
    public static String show(String path, int width, int height) {
        return ImageHelpUtil.show(path, width, height);
    }

    /**
     * 生成缩略图
     *
     * @param path
     * @param width
     * @param height
     * @return
     */
    public static String thumb(String path, int width, int height) {
        return ImageHelpUtil.thumb(path, width, height);
    }

    public static String showProjectFile(Files files) {

        if (files == null) {
            return "";
        }
        if (files.getFileType().contains("image")
                || files.getFileType().contains("pdf")) {
            return files.getNewPath();
        } else if (files.getFileType().contains("video")) {
            return "/video/" + files.getId();
        } else {
            return "/file-info/download?id=" + files.getId();
        }
    }

    /**
     * 格式化时间
     *
     * @param d
     * @return
     */
    public static String intDateFormat(Integer d, String format) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        if (d == null || d == 0) {
            return dateFormat.format(new Date());
        }
        return ViewHomeUtils.intToDateString(d, format);

    }

    /**
     * 显示图标
     *
     * @param path
     * @return
     */
    public static String fileIcon(String path) {

        String getPath = FileIconUtil.icon(path);
        String rootPath = FilesUtil.getRootDir();
        File file = new File(rootPath + getPath);
        if (file.exists()) {
            return getPath;
        }
        return FileIconUtil.icon(path);
    }


    /**
     * @param content
     * @return
     */
    public static List<String> encodeImages(String content) {
        return FilesUtil.encodeImages(content);
    }

    public static void main(String[] args) throws MalformedPatternException {
        new FfmpegUtils("/Users/suzhen/approot/www/zhuanti/2017zhongyao/01.flv")
                .setOutPath("/Users/suzhen/approot/www/zhuanti/2017zhongyao/01.png")
                .videoScreenshot();
    }

}
